package com.graduate.work.graduatework.Controller;

import com.graduate.work.graduatework.common.Result;
import com.graduate.work.graduatework.common.ResultStatus;
import com.graduate.work.graduatework.entity.User;
import com.graduate.work.graduatework.service.UserloginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

/**
 * @author: cuibingqi.
 * @date: 17/5/4 下午9:26.
 * @descripe:
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserloginService userloginService;

    /**
     * 用户注册接口
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("register")
    public String register(User user,ModelMap modelMap){

        Result<Boolean> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());
        Optional<User> isUser = Optional.ofNullable(user);
        if(!isUser.isPresent()){
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
        }
        result.setBody(userloginService.registe(user));
        modelMap.addAttribute("registerResult",result);

        return "registesuccess";

    }

    /**
     * 用户登录
     * @param userId
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("login")
    public Result<User> login(String userId,String password){//刷新本页面
        Result<User> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());
        Optional<String> isUserId = Optional.ofNullable(userId);
        Optional<String> isPassword = Optional.ofNullable(password);
        if(!isUserId.isPresent()||!isUserId.isPresent()){
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
            return result;
        }
        result.setBody(userloginService.login(userId,password));
        return result;
    }

    //查询用户信息

}
